package com.ng.desktop.expansionsa.view.articulo;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.DecimalFormat;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.commons.lang.StringUtils;

import com.jidesoft.swing.AutoCompletionComboBox;
import com.ng.desktop.expansionsa.model.Articulo;
import com.ng.desktop.expansionsa.persistence.ArticuloPersistence;
import com.ng.desktop.expansionsa.services.CustomNGListingService;
import com.ng.desktop.expansionsa.services.NGFactoryBean;
import com.ng.framework.util.NGUtil;
import com.ng.framework.view.desktop.NGEditDialog;

public class ArticuloEditDialog extends NGEditDialog<Articulo, ArticuloPersistence> {
	
	private static final long serialVersionUID = 4395713528968386788L;
	private static final DecimalFormat FORMATO_PRECIO = new DecimalFormat("0.00");
	
	private AutoCompletionComboBox comboBoxCategoria;
	private JTextField textFieldDescripcion;
	private JTextField textFieldPrecioDia;
	private JTextField textFieldPrecioSemana;
	private JTextField textFieldPrecioQuincena;
	private JTextField textFieldPrecioMes;
	
	private CustomNGListingService customNGListingService;
	
	public ArticuloEditDialog(Frame owner, ArticuloPersistence persistence) {
		this(owner, persistence, null);
	}

	public ArticuloEditDialog(Frame owner, ArticuloPersistence persistence, Articulo instance) {
		super(owner, persistence, instance);
		
		this.setTitle("Gestión de Artículo");
		this.setSize(new Dimension(300, 300));
	}

	@Override
	protected JPanel createDetailPanel() {
		JPanel detailPanel = new JPanel(new GridBagLayout());
		
		detailPanel.add(new JLabel("Categoría:"), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(this.getComboBoxCategoria(), new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 0), 0, 0));
		
		detailPanel.add(new JLabel("Descripción:"), new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(this.getTextFieldDescripcion(), new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 0), 0, 0));
		
		detailPanel.add(new JLabel("Precio por día:"), new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(this.getTextFieldPrecioDia(), new GridBagConstraints(1, 2, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 0), 0, 0));
		
		detailPanel.add(new JLabel("Precio por semana:"), new GridBagConstraints(0, 3, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(this.getTextFieldPrecioSemana(), new GridBagConstraints(1, 3, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 0), 0, 0));
		
		detailPanel.add(new JLabel("Precio por quincena:"), new GridBagConstraints(0, 4, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(this.getTextFieldPrecioQuincena(), new GridBagConstraints(1, 4, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 0), 0, 0));
		
		detailPanel.add(new JLabel("Precio por mes:"), new GridBagConstraints(0, 5, 1, 1, 0, 1, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(this.getTextFieldPrecioMes(), new GridBagConstraints(1, 5, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 0), 0, 0));
		
		return detailPanel;
	}
	
	private AutoCompletionComboBox getComboBoxCategoria() {
		if(comboBoxCategoria == null) {
			comboBoxCategoria = new AutoCompletionComboBox(this.getCategorias().toArray());
			comboBoxCategoria.setStrict(false);
			comboBoxCategoria.setStrictCompletion(true);
		}
		
		return comboBoxCategoria;
	}
	
	private JTextField getTextFieldDescripcion() {
		if(textFieldDescripcion == null) {
			textFieldDescripcion = new JTextField();
		}
		
		return textFieldDescripcion;
	}
	
	private JTextField getTextFieldPrecioDia() {
		if(textFieldPrecioDia == null) {
			textFieldPrecioDia = new JTextField(7);
		}
		
		return textFieldPrecioDia;
	}
	
	private JTextField getTextFieldPrecioSemana() {
		if(textFieldPrecioSemana == null) {
			textFieldPrecioSemana = new JTextField(7);
		}
		
		return textFieldPrecioSemana;
	}
	
	private JTextField getTextFieldPrecioQuincena() {
		if(textFieldPrecioQuincena == null) {
			textFieldPrecioQuincena = new JTextField(7);
		}
		
		return textFieldPrecioQuincena;
	}
	
	private JTextField getTextFieldPrecioMes() {
		if(textFieldPrecioMes == null) {
			textFieldPrecioMes = new JTextField(7);
		}
		
		return textFieldPrecioMes;
	}
	
	private List<String> getCategorias() {
		return this.getCustomNGListingService().getCategorias();
	}
	
	private CustomNGListingService getCustomNGListingService() {
		if(customNGListingService == null) {
			customNGListingService = (CustomNGListingService)NGFactoryBean.getInstance().getBean("customNGListingService");
		}
		
		return customNGListingService;
	}

	@Override
	protected void populateDetailPanel(Articulo instance) {
		this.getComboBoxCategoria().setSelectedItem(instance.getCategoria());
		this.getTextFieldDescripcion().setText(instance.getDescripcion());
		this.getTextFieldPrecioDia().setText(FORMATO_PRECIO.format(instance.getPrecioDia()));
		this.getTextFieldPrecioSemana().setText(FORMATO_PRECIO.format(instance.getPrecioSemana()));
		this.getTextFieldPrecioQuincena().setText(FORMATO_PRECIO.format(instance.getPrecioQuincena()));
		this.getTextFieldPrecioMes().setText(FORMATO_PRECIO.format(instance.getPrecioMes()));
	}

	@Override
	protected boolean validateInput() {
		boolean valid = true;
		
		if(this.getComboBoxCategoria().getSelectedItem() == null) {
			JOptionPane.showMessageDialog(this, "El campo 'Categoría' es obligatorio.", "Error", JOptionPane.ERROR_MESSAGE);
			this.getComboBoxCategoria().requestFocus();
			
			valid = false;
		} else if(StringUtils.isBlank(this.getTextFieldPrecioDia().getText())) {
			JOptionPane.showMessageDialog(this, "El campo 'Precio por día' es obligatorio.", "Error", JOptionPane.ERROR_MESSAGE);
			this.getTextFieldPrecioDia().requestFocus();
			
			valid = false;
		} else if(!NGUtil.validateDouble(this.getTextFieldPrecioDia().getText())) {
			JOptionPane.showMessageDialog(this, "El contenido del campo 'Precio por día' debe ser númerico.", "Error", JOptionPane.ERROR_MESSAGE);
			this.getTextFieldPrecioDia().requestFocus();
			
			valid = false;
		} else if(StringUtils.isBlank(this.getTextFieldPrecioSemana().getText())) {
			JOptionPane.showMessageDialog(this, "El campo 'Precio por semana' es obligatorio.", "Error", JOptionPane.ERROR_MESSAGE);
			this.getTextFieldPrecioSemana().requestFocus();
			
			valid = false;
		} else if(!NGUtil.validateDouble(this.getTextFieldPrecioSemana().getText())) {
			JOptionPane.showMessageDialog(this, "El contenido del campo 'Precio por semana' debe ser númerico.", "Error", JOptionPane.ERROR_MESSAGE);
			this.getTextFieldPrecioSemana().requestFocus();
			
			valid = false;
		} else if(StringUtils.isBlank(this.getTextFieldPrecioQuincena().getText())) {
			JOptionPane.showMessageDialog(this, "El campo 'Precio por quincena' es obligatorio.", "Error", JOptionPane.ERROR_MESSAGE);
			this.getTextFieldPrecioQuincena().requestFocus();
			
			valid = false;
		} else if(!NGUtil.validateDouble(this.getTextFieldPrecioQuincena().getText())) {
			JOptionPane.showMessageDialog(this, "El contenido del campo 'Precio por quincena' debe ser númerico.", "Error", JOptionPane.ERROR_MESSAGE);
			this.getTextFieldPrecioQuincena().requestFocus();
			
			valid = false;
		} else if(StringUtils.isBlank(this.getTextFieldPrecioMes().getText())) {
			JOptionPane.showMessageDialog(this, "El campo 'Precio por mes' es obligatorio.", "Error", JOptionPane.ERROR_MESSAGE);
			this.getTextFieldPrecioMes().requestFocus();
			
			valid = false;
		} else if(!NGUtil.validateDouble(this.getTextFieldPrecioMes().getText())) {
			JOptionPane.showMessageDialog(this, "El contenido del campo 'Precio por mes' debe ser númerico.", "Error", JOptionPane.ERROR_MESSAGE);
			this.getTextFieldPrecioMes().requestFocus();
			
			valid = false;
		}
		
		return valid;
	}

	@Override
	protected Articulo createNewInstance() {
		return new Articulo();
	}

	@Override
	protected void populateInstance(Articulo instance) {
		instance.setCategoria((String)this.getComboBoxCategoria().getSelectedItem());
		String descripcion = this.getTextFieldDescripcion().getText();
		instance.setDescripcion(StringUtils.isNotBlank(descripcion) ? descripcion : null);
		instance.setPrecioDia(Double.parseDouble(this.getTextFieldPrecioDia().getText()));
		instance.setPrecioMes(Double.parseDouble(this.getTextFieldPrecioMes().getText()));
		instance.setPrecioQuincena(Double.parseDouble(this.getTextFieldPrecioQuincena().getText()));
		instance.setPrecioSemana(Double.parseDouble(this.getTextFieldPrecioSemana().getText()));
	}

}
